package com.rinlink.gateway.jt808.configuration;

import com.rinlink.gateway.jt808.jt808.webhook.Jt808WebHookDispatch;
import com.rinlink.gateway.jt808.jt808.webhook.Jt808WebHookSessionListener;
import com.rinlink.gateway.jt808.service.Jt808GatewayWebHookService;
import com.rinlink.gateway.jt808.service.Jt808GatewayWebHookServiceImpl;
import com.rinlink.gateway.jt808.service.WebHookService;
import io.github.hylexus.jt808.boot.config.DefaultJt808ServerAutoConfigure;
import io.github.hylexus.jt808.dispatcher.RequestMsgDispatcher;
import io.github.hylexus.jt808.session.Jt808SessionManagerEventListener;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(Jt808GatewayWebHookProperties.class)
@AutoConfigureBefore(DefaultJt808ServerAutoConfigure.class)
@ConditionalOnProperty(prefix = "jt808.gateway.provide",name = "web-hook",matchIfMissing = true)
public class Jt808GatewayWebHookConfiguration {

    private final Jt808GatewayWebHookProperties jt808GatewayWebHookProperties;

    public Jt808GatewayWebHookConfiguration(Jt808GatewayWebHookProperties properties){
        this.jt808GatewayWebHookProperties = properties;
    }

    @Bean
    public Jt808GatewayWebHookService jt808GatewayWebHookService(WebHookService webHookService){
        return new Jt808GatewayWebHookServiceImpl(
                jt808GatewayWebHookProperties.getUrl(),
                jt808GatewayWebHookProperties.getTimeoutSeconds(),
                webHookService
        );
    }

    @Bean
    public RequestMsgDispatcher jt808RequestMsgDispatcher(Jt808GatewayWebHookService jt808GatewayWebHookService){
        return new Jt808WebHookDispatch(jt808GatewayWebHookService);
    }

    @Bean
    public Jt808SessionManagerEventListener jt808SessionManagerEventListener(Jt808GatewayWebHookService jt808GatewayWebHookService){
        return new Jt808WebHookSessionListener(jt808GatewayWebHookService);
    }


}
